/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaConsultaCliente.java
 *
 * 
 */

package Telas;
import BD.daoCliente;
import gerenciadorveiculos.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Gabriel
 */
public class telaConsultaCliente extends javax.swing.JFrame {

    /** Creates new form telaConsultaCliente */
    public telaConsultaCliente() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        CPFLabel = new javax.swing.JLabel();
        jTextCPF = new javax.swing.JTextField();
        pesquisarButton = new javax.swing.JButton();
        jButtonListarTodos = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Pesquisa Cliente");

        jPanel1.setName("jPanel1"); // NOI18N

        CPFLabel.setText("CPF:");
        CPFLabel.setName("CPFLabel"); // NOI18N

        jTextCPF.setName("jTextCPF"); // NOI18N

        pesquisarButton.setText("Pesquisar");
        pesquisarButton.setName("pesquisarButton"); // NOI18N
        pesquisarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarButtonActionPerformed(evt);
            }
        });

        jButtonListarTodos.setText("Listar Todos");
        jButtonListarTodos.setName("jButtonListarTodos"); // NOI18N
        jButtonListarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonListarTodosActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(CPFLabel)
                .add(12, 12, 12)
                .add(jTextCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pesquisarButton)
                .add(38, 38, 38)
                .add(jButtonListarTodos)
                .addContainerGap(144, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(CPFLabel)
                    .add(pesquisarButton)
                    .add(jButtonListarTodos))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        jSeparator1.setName("jSeparator1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nome", "CPF", "Telefone", "Endereco"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE)
                .addContainerGap())
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 989, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(458, 458, 458))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(86, 86, 86)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pesquisarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarButtonActionPerformed
        daoCliente cli = new daoCliente();

        String cpf = jTextCPF.getText().toString();
        
        Vector<Cliente> vet = new Vector(cli.buscar(cpf));
        if(vet.isEmpty())
            JOptionPane.showMessageDialog(null, "Cliente nao Encontrado", "", 0);
        DefaultTableModel model = new DefaultTableModel(new Object[][]{}
                                , new Object[]{"Nome", "CPF","Telefone","Endereco"});
        
        for (Cliente cliente : vet) {
            model.addRow(new String[]{cliente.getNome(), cliente.getCPF(),cliente.getTelefone(),cliente.getEndereco()});
        }
        jTable1.setModel(model);
        
    }//GEN-LAST:event_pesquisarButtonActionPerformed

    private void jButtonListarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonListarTodosActionPerformed
        daoCliente cli = new daoCliente();
        
        Vector<Cliente> vet = new Vector(cli.buscarTodos());
        if(vet.isEmpty())
            JOptionPane.showMessageDialog(null, "Cliente nao Encontrado", "", 0);
        DefaultTableModel model = new DefaultTableModel(new Object[][]{}
                                , new Object[]{"Nome", "CPF","Telefone","Endereco"});

        for (Cliente cliente : vet) {
            model.addRow(new String[]{cliente.getNome(), cliente.getCPF(),cliente.getTelefone(),cliente.getEndereco()});
        }
        jTable1.setModel(model);
    }//GEN-LAST:event_jButtonListarTodosActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaConsultaCliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CPFLabel;
    private javax.swing.JButton jButtonListarTodos;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextCPF;
    private javax.swing.JButton pesquisarButton;
    // End of variables declaration//GEN-END:variables

}
